﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Com.SoundVideo;
using System.Net;

namespace Com
{
    public partial class MainFrm : Form
    {
        string m_usstuid="";
        string m_accounts = "";
        string m_stuname = "";
        public MainFrm()
        {
            InitializeComponent();

            StringBuilder list = new StringBuilder();
            list.AppendLine("1：选择要拍照的学生，在点击catch拍照并入库！");
            list.AppendLine("2：照片存放在images文件夹中！");
            list.AppendLine("3：将图片上传到服务器上对应的文件夹下！");
            list.AppendLine("4：生成照片大小：320*240");
            lab_msg.Text = list.ToString();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < vcx.GetVideoDeviceCount(); i++){
                cmb_usb.Items.Add(vcx.GetVideoDeviceName(i));
            }
            cmb_usb.SelectedIndex = 0;
            UserMessage.GetGrade(cmb_grade);
            btnScroll.Enabled = false;
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            vcxOrVCP();
            timer1.Enabled = true;
        }
        private void btnScroll_Click(object sender, EventArgs e)
        {
            if (m_usstuid.Length > 0&&m_accounts.Length>0)
            {
                Image img = vcx.GrabFrame();
                string output = Application.StartupPath + "\\images\\";
                img.Save(output + "" + m_accounts + ".jpg");
                UserMessage.UpdateStudentImg(m_usstuid, m_accounts + ".jpg");
                MessageBox.Show("拍照成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                dataGridView1.DataSource = UserMessage.GetClassStudent(cmb_class.SelectedValue.ToString());
            }else{
                MessageBox.Show("请选中旁边的学生在拍照！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void cmb_usb_SelectedIndexChanged(object sender, EventArgs e)
        {
            vcxOrVCP();
        }
        public void vcxOrVCP()
        {
            if (vcx.Connected) vcx.Connected = false;
            vcx.VideoDeviceIndex = cmb_usb.SelectedIndex;
            vcx.Connected = true;
            vcx.Preview = true;
        }

        private void cmb_grade_SelectedIndexChanged(object sender, EventArgs e)
        {
            string gradeid = cmb_grade.SelectedValue.ToString();
            if (gradeid.EndsWith("System.Data.DataRowView") == false){
                UserMessage.GetGradeClass(gradeid, cmb_class);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            vcxOrVCP();
        }

        private void btnClose_Click(object sender, EventArgs e){
            timer1.Enabled = false;
        }

        private void cmb_class_SelectedIndexChanged(object sender, EventArgs e)
        {
            string classid = cmb_class.SelectedValue.ToString();
            if (classid.EndsWith("System.Data.DataRowView") == false){
                dataGridView1.DataSource = UserMessage.GetClassStudent(classid);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentCell.RowIndex;
                dataGridView1.Rows[index].Selected = true;
                m_usstuid = this.dataGridView1.Rows[index].Cells["usstuid"].Value.ToString();
                m_accounts = this.dataGridView1.Rows[index].Cells["accounts"].Value.ToString();
                m_stuname = this.dataGridView1.Rows[index].Cells["stuname"].Value.ToString();
                label2.Text = "当前选中的编号: " + m_usstuid + " 姓名: " + m_stuname + " 学号: " + m_accounts;
                btnScroll.Enabled = true;
            }
            catch { 
            }
        }

        private void MainFrm_Click(object sender, EventArgs e)
        {
            
        }

        private void cmb_grade_MouseEnter(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void dataGridView1_MouseEnter(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void MainFrm_MouseEnter(object sender, EventArgs e)
        {
            if (m_usstuid.Length > 0)
            {
                btnScroll.Enabled = true;
                timer1.Enabled = true;
                label2.Text = "当前选中的编号: " + m_usstuid + " 姓名: " + m_stuname + " 学号: " + m_accounts;
            }
            else {
                label2.Text = "当前没有选中学生!";
            }
            
        }

        private void cmb_class_MouseEnter(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void lab_msg_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentCell.RowIndex;
                dataGridView1.Rows[index].Selected = false;
                m_usstuid = "";
                m_accounts = "";
                m_stuname = "";
                label2.Text = "当前没有选中学生!";
                btnScroll.Enabled = false;
            }
            catch { }
        }
    }
}
